/// 轮播图
const carousel = require("express").Router()
const {rules, valid} = require("../../utils/rules");
const handlerList = require("../../handler/handlerList");
const {save, sqlRows, sqlUpdate, sqlDelete} = require("../../sql/rows");
const carouselSave = [
    {
        name: 'carousel_name',
        message: "轮播图名称"
    },
    {
        name: 'carousel_url',
        message: "轮播图链接地址"
    },
    {
        name: 'carousel_order',
        message: "轮播图排序"
    },
    {
        name: 'carousel_img',
        message: "轮播图图片"
    }
]
const carouselUpdate = [].concat(carouselSave, [
    {
        name: 'id',
        message: "轮播图id"
    }
])

carousel.post("/save", rules(carouselSave), valid, async (req, res) =>{
    try {
        const data = req.body
        const sqlSave = save('sms_carousel', data)
        const sqlList = sqlRows('sms_carousel', {
            SELECT: 'carousel_name',
            data: {
                carousel_name: data.carousel_name
            }
        })

        const list = await req.db.query(sqlList)
        if (list.length) {
            return res.status(200).send({
                code: 400,
                message: "轮播图名称重复无法添加",
                data: ""
            })
        }
        await req.db.query(sqlSave)
        return res.status(200).send({
            code: 200,
            message: "添加成功",
            data: ""
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})

carousel.post("/update", rules(carouselUpdate), valid, async (req, res) => {
    try {
        const {
            carousel_name,
            id,
            carousel_url,
            carousel_order,
            carousel_img
        } = req.body

        const data = {
            carousel_name,
            carousel_url,
            carousel_order: carousel_order.toFixed(2),
            carousel_img
        }

        const sql = sqlUpdate('sms_carousel', {data, key:{id}})
        const sqlList = sqlRows('sms_carousel', {
            SELECT: 'id',
            data: {
                id: id
            }
        })
        console.log(sqlList)
        const list = await req.db.query(sqlList)
        if (list.length) {
            // console.log(sql)
            await req.db.query(sql)

            return res.status(200).send({
                code: 200,
                message: "修改成功",
                data: ""
            })
        }
        return res.status(200).send({
            code: 400,
            message: "当前没有该轮播图数据无法修改",
            data: ""
        })
    } catch (e) {
        // console.log(e)
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})

carousel.post("/list", (req, res) => handlerList(req ,res, 'sms_carousel'))

carousel.delete("/delete", async (req, res) => {
    try {
        if (req.query.id) {
            const id = req.query.id
            const sql = sqlRows('sms_carousel', {data: {id}})
            const carousels = await req.db.query(sql)
            if (carousels.length) {
                const data = {id}
                await req.db.query(sqlDelete('sms_carousel', {data}))
                return res.status(200).send({
                    code: 200,
                    data: "",
                    message: "删除成功"
                })
            }
            return res.status(200).send({
                code: 400,
                message: '当前轮播图已删除',
                data: ''
            })
        }
        return res.status(200).send({
            code: 400,
            message: '请传入轮播图id',
            data: ''
        })
    } catch (e) {
        return res.status(500).send({
            code: 500,
            message: '系统错误',
            data: ''
        })
    }
})

module.exports = carousel